package aar.test.tessocr.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import aar.test.tessocr.model.UnexporderListModel;
import clx.tessocr.ocreng.R;

/**
 * Created by 149-201605 on 2016/8/1.
 */

/*
* 主界面显示 dialog的资源适配器
* */
public class ExporderListAdapter extends BaseAdapter {

    private List<UnexporderListModel> mList;

    private Context mContext;



    public ExporderListAdapter(Context mContext, List<UnexporderListModel> mList) {
        this.mList = mList;
        this.mContext = mContext;


    }



    @Override
    public int getCount() {
        int count = 0;
        if (null != mList && !mList.isEmpty()) {
            count = mList.size();
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder showDialogHolder = null;
        if (null == convertView) {
            showDialogHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.exporder_list_item, null);

            showDialogHolder.order_id=convertView.findViewById(R.id.order_id);
            showDialogHolder.order_phone=convertView.findViewById(R.id.order_phone);
            showDialogHolder.cabinet_name=convertView.findViewById(R.id.cabinet_name);
            showDialogHolder.grid_type=convertView.findViewById(R.id.grid_type);
            showDialogHolder.grid_address=convertView.findViewById(R.id.grid_address);
            showDialogHolder.cabinet_address=convertView.findViewById(R.id.cabinet_address);
            showDialogHolder.cabinet_statues=convertView.findViewById(R.id.cabinet_statues);
            showDialogHolder.cabinet_time=convertView.findViewById(R.id.cabinet_time);
            showDialogHolder.order_grid_open=convertView.findViewById(R.id.order_grid_open);
            showDialogHolder.order_grid_quhui=convertView.findViewById(R.id.order_grid_quhui);
            showDialogHolder.order_play_number_phone=convertView.findViewById(R.id.order_play_number_phone);
            convertView.setTag(showDialogHolder);
        } else {
            showDialogHolder = (ViewHolder) convertView.getTag();
        }

         final int pos=position;


        try{

            showDialogHolder.order_id.setText(mList.get(position).getExpress_order());


            showDialogHolder.order_phone.setText(mList.get(position).getPhone());


            showDialogHolder.cabinet_name.setText(mList.get(position).getCabinetName());

            showDialogHolder.grid_type.setText(mList.get(position).getGridType());


            showDialogHolder.grid_address.setText(mList.get(position).getGridNum()+"格");

            showDialogHolder.cabinet_address.setText(mList.get(position).getCabinetAddress());


            showDialogHolder.cabinet_statues.setText(mList.get(position).getStatus());


            showDialogHolder.cabinet_time.setText(mList.get(position).getCreated_at());

            if(TextUtils.equals(mList.get(position).getStatus(),"已取件")){

                showDialogHolder.order_grid_open.setVisibility(View.GONE);
                showDialogHolder.order_grid_quhui.setVisibility(View.GONE);
                showDialogHolder.order_play_number_phone.setVisibility(View.GONE);




            }else{
                showDialogHolder.order_grid_open.setVisibility(View.GONE);
                showDialogHolder.order_grid_quhui.setVisibility(View.VISIBLE);
                showDialogHolder.order_play_number_phone.setVisibility(View.VISIBLE);

            }


            showDialogHolder.order_grid_open.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(null!=mViewClickListener){
                        mViewClickListener.gridOpen(pos);
                    }

                }
            });


            showDialogHolder.order_grid_quhui.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null!=mViewClickListener){
                        mViewClickListener.gridTackBack(pos);
                    }
                }
            });



            showDialogHolder.order_play_number_phone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(!TextUtils.isEmpty(mList.get(pos).getPhone())){


                        Intent intent=new Intent();
                        intent.setAction(Intent.ACTION_CALL);
                        intent.setData(Uri.parse("tel:" + mList.get(pos).getPhone()));
                        //开启系统拨号器
                        mContext.startActivity(intent);


                    }else{
                        Toast.makeText(mContext, "电话号码为空", Toast.LENGTH_SHORT).show();


                    }
                }
            });










        }catch (Exception e){
            Log.e("ExporderListAdapter", "Exception-----------------"+e.getMessage());
            e.printStackTrace();
        }









        return convertView;
    }


    class ViewHolder {
        TextView order_id;
        TextView order_phone;
        TextView cabinet_name;
        TextView grid_type;
        TextView grid_address;
        TextView cabinet_address;
        TextView cabinet_statues;
        TextView cabinet_time;
        TextView order_grid_open;
        TextView order_grid_quhui;
        TextView order_play_number_phone;
    }



    public interface ViewClickListener{
        void gridOpen(int pos);
        void gridTackBack(int pos);
    }

    public void setViewClickListener(ViewClickListener mViewClickListener){
        this.mViewClickListener=mViewClickListener;

    }


    private ViewClickListener mViewClickListener;
}


